package com.kxz.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kxz.pojo.ResultMessage;

//把服务器的端的数据转换为json格式的字符串
public class MessageUtils { //封装服务器端要发送的数据
    public static String getMessage(boolean isSystemMessage,String fromName,Object message){
        try {
            ResultMessage resultMessage=new ResultMessage();
            resultMessage.setSystem(isSystemMessage);
            resultMessage.setMessage(message);
            if (fromName!=null){
                resultMessage.setFromName(fromName);
            }
            //转换为Json格式
            ObjectMapper mapper=new ObjectMapper();
            return mapper.writeValueAsString(resultMessage);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        return null;
    }
}
